package fr.sunnydale.android.geek.entity.factory;

import android.content.res.Resources;
import fr.sunnydale.android.geek.Dream;
import fr.sunnydale.android.geek.sprite.ISprite;
import fr.sunnydale.android.geek.sprite.impl.Sprite;

public class SpriteFactory {

    public static ISprite getSprite(Resources res, int id) {
        Class<? extends Sprite> spriteClass = null;
        try {
            spriteClass = extracted(id);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ISprite spriteToReturn = null;
        try {
            spriteToReturn = spriteClass.newInstance();
            spriteToReturn.init(res);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return spriteToReturn;
    }

    @SuppressWarnings("unchecked")
    private static Class<? extends Sprite> extracted(int id) throws ClassNotFoundException {
        return (Class<? extends Sprite>) Class.forName(Dream.mResources.getString(id));
    }
}
